package guoan.web.weixin;

import guoan.Exception.CheckDataException;
import guoan.Exception.RestException;
import guoan.core.common.WebConfig;
import guoan.core.dto.RestResponse;
import guoan.domain.Coach;
import guoan.domain.CoachConfig;
import guoan.domain.CoachItem;
import guoan.domain.RandomKey;
import guoan.domain.Student;
import guoan.domain.Train;
import guoan.service.CoachConfigService;
import guoan.service.CoachService;
import guoan.service.RandomKeyService;
import guoan.service.StudentService;
import guoan.service.TrainService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionTimedOutException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/weixin/coach")
public class WeiXinCoachController {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private CoachService coachService;

	@Autowired
	private TrainService trainService;

	@Autowired
	private StudentService studentService;

	@Autowired
	private RandomKeyService randomKeyService;
	@Autowired
	private CoachConfigService coachConfigService;

	/** 跳转到考试主页 */
	/**
	 * @param userType
	 *            用户类型，1：学员，2：教练
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "index_{userType}", method = RequestMethod.GET)
	public String index(@PathVariable("userType") Long userType, HttpServletRequest request, Model model) {
		Preconditions.checkNotNull(userType, "抱歉，参数错误！");
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			if (userType.intValue() == 2) {// 教练
				if (trainService.findByOpenid(openid) == null) {// 进行教练绑定
					return "redirect:/weixin/train/oauthTrain";
				}

			} else if (userType.intValue() == 1) {// 学员
				Student student = studentService.findByStudentOpenid(openid);
				if (student == null) {// 进行学员绑定
					return "redirect:/weixin/student/oauthStudent";
				} else {
					// 判断有那些预约权限
					List<RandomKey> randomKeys = randomKeyService.findByStudentId(student.getId());
					if (randomKeys != null && randomKeys.size() > 0) {
						int coachType = 0; // 0 是没有任何权限，但是不可能出现，2是科目2 ,3是科目3,5
											// 是全部都有
						for (RandomKey randomKey : randomKeys) {
							if (randomKey.getCoachType().intValue() == 2) {
								coachType += 2;
							}

							if (randomKey.getCoachType().intValue() == 3) {
								coachType += 3;
							}
						}
						model.addAttribute("coachType", coachType);
					} else {
						return "redirect:/weixin/student/oauthStudent";
					}
				}
			}
		} else {
			throw new RestException("请在微信中打开");
		}
		model.addAttribute("userType", userType);
		return "/weixin/coach/index";
	}

	/**
	 * 用户或者教练查看练车信息
	 * 
	 * @param userType
	 *            用户类型
	 * @param type
	 *            科目二或者三
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "coach_{userType}_{type}", method = RequestMethod.GET)
	public String coach(@PathVariable("userType") Long userType, @PathVariable("type") Long type,
			HttpServletRequest request, Model model) {
		Long trainId = null;
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			if (userType.intValue() == 2) {// 教练
				Train train = trainService.findByOpenid(openid);
				if (train == null) {// 进行教练绑定
					return "redirect:/weixin/train/oauthTrain";
				} else {
					trainId = train.getId();
					// 打开对应栏目进行自动生成，生成包括今天的五天的考试信息，采用默认时间段
					RestResponse<Boolean> rr = coachService.autoAdd(trainId, type);
					if (rr.getStatus() == RestResponse.STATUS_ERROR) {
						model.addAttribute("msg", rr.getMsg());
						return "/weixin/msg";
					}
					List<Coach> coachLists = coachService.findCoach(type, userType, trainId);
					for (Coach coach : coachLists) {
						// 预约数量
						Long isyy = coachService.findCoachItemCountByCoachId(coach.getId());
						coach.setIsyy(isyy.longValue());
					}

					model.addAttribute("coachLists", coachLists);
				}

			} else if (userType.intValue() == 1) {// 学员
				Student student = studentService.findByStudentOpenid(openid);
				if (student == null) {// 进行学员绑定
					return "redirect:/weixin/student/oauthStudent";
				} else {
					RandomKey randomKey = randomKeyService.findByStudentIdAndType(student.getId(), type);
					trainId = randomKey.getTrainId();
					// 未来五天的预约情况
					List<Coach> coachLists = coachService.findCoach(type, userType, trainId);
					if (type.intValue() == 2) {// 科目二
						for (Coach coach : coachLists) {
							// 预约是否已经满了
							List<String> ohs = coachService.findRightItem(coach.getId());
							if (ohs.size() == 0) {// 可用数是0，则预约已满
								coach.setIsm(1L);
							}
							// 是否已经预约
							Long isyy = coachService.findCoachItemCountByStudentIdAndCoachId(student.getId(),
									coach.getId());
							coach.setIsyy(isyy.longValue());
						}
					} else {// 科目三
						for (Coach coach : coachLists) {

							String openhours = coach.getOpenHours();
							List<String> openHoursIter = Splitter.on(",").splitToList(openhours);

							String defaultThreeCoachNos = "2,1,2";
							String threeCoachConfig = coach.getThreeCoachConfig();
							List<String> threeCoachNos = Lists.newArrayList();
							if (threeCoachConfig != null) {
								defaultThreeCoachNos = threeCoachConfig;
							}
							threeCoachNos = Splitter.on(",").trimResults().splitToList(defaultThreeCoachNos);
							// 判断各个时间段是否完成预约
							for (String oh : openHoursIter) {
								int cc = coachService.findCoachItemCountByCoachIdAndOpenHours(coach.getId(), oh)
										.intValue();
								if ((cc >= Integer.valueOf(threeCoachNos.get(0)).intValue() && oh.equals("上午"))
										|| (cc >= Integer.valueOf(threeCoachNos.get(2)).intValue() && oh.equals("晚上"))
										|| (cc >= Integer.valueOf(threeCoachNos.get(1)).intValue() && oh.equals("下午"))) {
									coach.setIsm(1L);
								}
							}

							Long isyy = coachService.findCoachItemCountByStudentIdAndCoachId(student.getId(),
									coach.getId());
							coach.setIsyy(isyy.longValue());
						}
					}
					model.addAttribute("coachLists", coachLists);
				}
			}
		} else {
			throw new RestException("请在微信中打开");
		}

		model.addAttribute("userType", userType);
		model.addAttribute("type", type);
		return "/weixin/coach/coach";
	}

	/**
	 * 跳转到更新预约练车信息
	 * 
	 * @param coachId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String toUpdate(Long coachId, Model model, HttpServletRequest request) {
		Preconditions.checkNotNull(coachId, "抱歉，参数错误！");
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			if (trainService.findByOpenid(openid) == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
		} else {
			throw new RestException("请在微信中打开");
		}
		Coach coach = coachService.findOne(coachId);
		if (coach == null) {
			model.addAttribute("msg", "当前信息不存在");
			return "/weixin/msg";
		}
		model.addAttribute("coach", coach);
		List<String> openhours = Splitter.on(",").trimResults().splitToList(coach.getOpenHours());

		// 如果是科目二则判断需要更新的信息没有过期
		// 根据配置的时间点进行更新
		if (coach.getType().intValue() == 2) {
			int jg = Integer.valueOf(coach.getJg()).intValue();
			List<String> ohs = Lists.newArrayList();
			for (int i = 0; i < 24; i++) {
				ohs.add(i + ":00");
				int isMAX = 60;
				int start = 0;
				while (true) {
					start = start + jg;
					int f = start;
					if (start == isMAX) {
						break;
					}
					ohs.add(i + ":" + f);
				}
			}
			model.addAttribute("ohs", ohs);
		} else {
			String defaultThreeCoachNos = "2,1,2";
			String threeCoachConfig = coach.getThreeCoachConfig();
			if (threeCoachConfig != null) {
				defaultThreeCoachNos = threeCoachConfig;
			}
			List<String> threeCoachNos = Splitter.on(",").trimResults().splitToList(defaultThreeCoachNos);
			model.addAttribute("threeCoachNos", threeCoachNos);
		}

		model.addAttribute("openhours", openhours);

		return "/weixin/coach/update";
	}

	/** 跳转到考试主页 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Coach coach, Model model, HttpServletRequest request) {
		Preconditions.checkNotNull(coach, "抱歉，参数错误！");
		Preconditions.checkNotNull(coach.getJg(), "抱歉，参数错误！");
		Preconditions.checkNotNull(coach.getId(), "抱歉，参数错误！");

		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			Train train = trainService.findByOpenid(openid);
			if (train == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
			coach.setTrainId(train.getId());
		} else {
			throw new RestException("请在微信中打开");
		}

		RestResponse<Coach> rr = coachService.add(coach);
		if (rr.getStatus() == RestResponse.STATUS_OK) {
			model.addAttribute("msg", "修改信息成功，正在跳转");
			model.addAttribute("url", "/weixin/coach/coach_2_" + coach.getType());
			return "/weixin/msg";
		} else {
			model.addAttribute("msg", rr.getMsg());
			model.addAttribute("url", "/weixin/coach/update?coachId=" + coach.getId());
			return "/weixin/msg";
		}

	}

	@RequestMapping(value = "item", method = RequestMethod.GET)
	public String item(Long coachId, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			if (studentService.findByStudentOpenid(openid) == null) {// 进行学员绑定
				return "redirect:/weixin/student/oauthStudent";
			}
		} else {
			throw new RestException("请在微信中打开");
		}

		Coach coach = coachService.findOne(coachId);
		if (coach == null) {
			model.addAttribute("msg", "当前信息不存在");
			return "/weixin/msg";
		}
		if (coach.getStatus().intValue() == 0) {
			model.addAttribute("msg", "当前预约计划已经取消，请更换其他日期进行预约");
			model.addAttribute("url", "/weixin/coach/coach_1_" + coach.getType());
			return "/weixin/msg";
		}
		List<String> itemHours = coachService.findRightItem(coachId);
		model.addAttribute("itemHours", itemHours);
		model.addAttribute("coach", coach);
		return "/weixin/coach/coachItem";
	}

	/**
	 * 教练查看预约列表
	 * 
	 * @param coachId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "itemShow", method = RequestMethod.GET)
	public String itemShow(Long coachId, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			if (trainService.findByOpenid(openid) == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
		} else {
			throw new RestException("请在微信中打开");
		}

		Coach coach = coachService.findOne(coachId);
		if (coach == null) {
			model.addAttribute("msg", "当前信息不存在");
			return "/weixin/msg";
		}

		if (coach.getType().intValue() == 2) {
			List<String> ohs = coachService.findRightItem(coachId);
			model.addAttribute("ohs", ohs);
		} else if (coach.getType().intValue() == 3) {
			List<String> ohs = Lists.newArrayList();
			String openhours = coach.getOpenHours();
			List<String> openHoursIter = Splitter.on(",").splitToList(openhours);

			String defaultThreeCoachNos = "2,1,2";
			String threeCoachConfig = coach.getThreeCoachConfig();
			List<String> threeCoachNos = Lists.newArrayList();
			if (threeCoachConfig != null) {
				defaultThreeCoachNos = threeCoachConfig;
			}
			threeCoachNos = Splitter.on(",").trimResults().splitToList(defaultThreeCoachNos);

			// 判断各个时间段是否完成预约
			for (String oh : openHoursIter) {
				int cc = coachService.findCoachItemCountByCoachIdAndOpenHours(coach.getId(), oh).intValue();
				if ((cc < Integer.valueOf(threeCoachNos.get(0)).intValue() && oh.equals("上午"))
						|| (cc < Integer.valueOf(threeCoachNos.get(2)).intValue() && oh.equals("晚上"))
						|| (cc < Integer.valueOf(threeCoachNos.get(1)).intValue() && oh.equals("下午"))) {
					ohs.add(oh);
				}
			}
			model.addAttribute("ohs", ohs);
		}

		List<CoachItem> coachItems = coachService.findCoachItemByCoachId(coachId);
		model.addAttribute("coachItems", coachItems);
		model.addAttribute("coach", coach);
		return "/weixin/coach/coachItemShow";
	}

	@RequestMapping(value = "addYY", method = RequestMethod.GET)
	public String addYY(String itemHours, Long coachId, Model model, HttpServletRequest request) {
		try {
			this.log.debug("{},{},{}",itemHours,coachId,new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
			String rp = coachService.addYY(itemHours, coachId, model, request);
			this.log.debug("return {},model {}",rp,model);

			return rp;
		} catch (CheckDataException e) {
			// TODO Auto-generated catch block
			this.log.debug("当前考试时间已被预约:{}",e.getMessage());
			model.addAttribute("msg", "当前考试时间已被预约，请重新尝试别的时间");
			model.addAttribute("url", "/weixin/coach/item?coachId=" + coachId);
			return "/weixin/msg";
		}
	}

	@RequestMapping(value = "cleanCoach", method = RequestMethod.GET)
	public String cleanCoach(Long coachId, Model model, HttpServletRequest request) {
		Preconditions.checkNotNull(coachId, "抱歉，参数错误！");
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			Train train = trainService.findByOpenid(openid);
			if (train == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
		} else {
			throw new RestException("请在微信中打开");
		}
		RestResponse<Coach> rr = coachService.cleanCoach(coachId);
		if (rr.getStatus() == RestResponse.STATUS_ERROR) {
			model.addAttribute("msg", rr.getMsg());
		} else {
			model.addAttribute("msg", "计划状态更改成功");
		}
		model.addAttribute("url", "/weixin/coach/coach_2_" + rr.getData().getType());
		return "/weixin/msg";

	}

	@RequestMapping(value = "cleanItemCoach", method = RequestMethod.GET)
	public String cleanItemCoach(Long coachItemId, Model model, HttpServletRequest request) {
		Preconditions.checkNotNull(coachItemId, "抱歉，参数错误！");
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			Train train = trainService.findByOpenid(openid);
			if (train == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
		} else {
			throw new RestException("请在微信中打开");
		}
		RestResponse<Long> rr = coachService.cleanItemCoach(coachItemId);
		if (rr.getStatus() == RestResponse.STATUS_ERROR) {
			model.addAttribute("msg", rr.getMsg());

		} else {
			model.addAttribute("msg", "预约取消成功");
		}
		model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + rr.getData());
		return "/weixin/msg";

	}

	/**
	 * @Title：addTrainYY
	 * @Description: TODO
	 * @author Administrator
	 * @date 2015年5月11日 下午4:15:27
	 * @param itemHours
	 * @param coachId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addTrainYY", method = RequestMethod.GET)
	public String addTrainYY(String itemHours, Long coachId, Model model, HttpServletRequest request) {
		Preconditions.checkNotNull(itemHours, "抱歉，参数错误！");
		Preconditions.checkNotNull(coachId, "抱歉，参数错误！");
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			Train train = trainService.findByOpenid(openid);
			if (train == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
		} else {
			throw new RestException("请在微信中打开");
		}
		Coach coach = coachService.findOne(coachId);

		Long coachItemCount = coachService.findCoachItemCountByCoachIdAndOpenHours(coachId, itemHours);
		if (coach.getType() == 2) {
			if (coachItemCount.intValue() != 0) {
				model.addAttribute("msg", "当前练车时间已经被预约");
				model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + coachId);
				return "/weixin/msg";
			}
		} else if (coach.getType() == 3) {
			// 判断各个时间段是否完成预约
			int cc = coachService.findCoachItemCountByCoachIdAndOpenHours(coach.getId(), itemHours).intValue();

			// 判断各个时间段是否完成预约
			String defaultThreeCoachNos = "2,1,2";
			String threeCoachConfig = coach.getThreeCoachConfig();
			List<String> threeCoachNos = Lists.newArrayList();
			if (threeCoachConfig != null) {
				defaultThreeCoachNos = threeCoachConfig;
				threeCoachNos = Splitter.on(",").trimResults().splitToList(defaultThreeCoachNos);
			}
			// 科目三，上午2个，下午1个，晚上2个
			if (cc >= Integer.valueOf(threeCoachNos.get(0)).intValue() && itemHours.equals("上午")) {
				model.addAttribute("msg", "当前练车时间已经被预约");
				model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + coachId);
				return "/weixin/msg";
			}
			if (cc >= Integer.valueOf(threeCoachNos.get(2)).intValue() && itemHours.equals("晚上")) {
				model.addAttribute("msg", "当前练车时间已经被预约");
				model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + coachId);
				return "/weixin/msg";
			}
			if (cc >= Integer.valueOf(threeCoachNos.get(1)).intValue() && itemHours.equals("下午")) {
				model.addAttribute("msg", "当前练车时间已经被预约");
				model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + coachId);
				return "/weixin/msg";
			}
		}
		model.addAttribute("coach", coach);
		model.addAttribute("itemHours", itemHours);
		model.addAttribute("coachId", coachId);
		return "/weixin/coach/addTrainYY";
	}

	@RequestMapping(value = "addTrainYY", method = RequestMethod.POST)
	public String addTrainYY(String itemHours, CoachItem coachItem, Model model, HttpServletRequest request) {
		Preconditions.checkNotNull(itemHours, "抱歉，参数错误！");
		Preconditions.checkNotNull(coachItem, "抱歉，参数错误！");
		Preconditions.checkNotNull(coachItem.getCoachId(), "抱歉，参数错误！");
		Preconditions.checkNotNull(coachItem.getStudentName(), "抱歉，参数错误！");
		Preconditions.checkNotNull(coachItem.getContactNumberF(), "抱歉，参数错误！");
		HttpSession session = request.getSession();
		Train train = null;
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) != null) {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			train = trainService.findByOpenid(openid);
			if (train == null) {// 进行教练绑定
				return "redirect:/weixin/train/oauthTrain";
			}
		} else {
			throw new RestException("请在微信中打开");
		}
		Coach coach = coachService.findOne(coachItem.getCoachId());

		Long coachItemCount = coachService.findCoachItemCountByCoachIdAndOpenHours(coachItem.getCoachId(), itemHours);
		if (coach.getType() == 2) {
			if (coachItemCount.intValue() != 0) {
				model.addAttribute("msg", "当前练车时间已经被预约");
				model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + coachItem.getCoachId());
				return "/weixin/msg";
			}
		} else if (coach.getType() == 3) {
			// 判断各个时间段是否完成预约
			String defaultThreeCoachNos = "2,1,2";
			String threeCoachConfig = coach.getThreeCoachConfig();
			List<String> threeCoachNos = Lists.newArrayList();
			if (threeCoachConfig != null) {
				defaultThreeCoachNos = threeCoachConfig;
				threeCoachNos = Splitter.on(",").trimResults().splitToList(defaultThreeCoachNos);
			}
			// 科目三，上午2个，下午1个，晚上2个
			if (coachItemCount.intValue() >= Integer.valueOf(threeCoachNos.get(0)).intValue() && itemHours.equals("上午")) {
				model.addAttribute("msg", "您所选择的练车时间已经被别人抢用");
				model.addAttribute("url", "/weixin/coach/item?coachId=" + coachItem.getCoachId());
				return "/weixin/msg";
			}
			if (coachItemCount.intValue() >= Integer.valueOf(threeCoachNos.get(2)).intValue() && itemHours.equals("晚上")) {
				model.addAttribute("msg", "您所选择的练车时间已经被别人抢用");
				model.addAttribute("url", "/weixin/coach/item?coachId=" + coachItem.getCoachId());
				return "/weixin/msg";
			}
			if (coachItemCount.intValue() >= Integer.valueOf(threeCoachNos.get(1)).intValue() && itemHours.equals("下午")) {
				model.addAttribute("msg", "您所选择的练车时间已经被别人抢用");
				model.addAttribute("url", "/weixin/coach/item?coachId=" + coachItem.getCoachId());
				return "/weixin/msg";
			}
		}
		// 可用的预约
		CoachItem coachItemOld = coachService.findCoachItemByCoachIdAndOpenHours(coachItem.getCoachId(), itemHours);
		if (coachItemOld == null) {
			coachItemOld = coachItem;
		}
		coachItemOld.setCoachType(coach.getType());
		coachItemOld.setExamTime(coach.getCoachTime());

		DateTime now = new DateTime();
		coachItemOld.setJoinTime(now);

		coachItemOld.setTrainId(coach.getTrainId());
		coachItemOld.setTrainName(train.getTrainName());
		coachItemOld.setTrainTel(train.getTrainTel());

		String studentKey = randomKeyService.addRandomKey(coachItemOld.getTrainId(), coachItemOld.getCoachType(), 1L);
		coachItemOld.setStudentKey(studentKey);
		coachItemOld.setStatus(CoachItem.STATUS_DEFALUT); // 默认的状态为正常

		coachService.addCoachItem(coachItemOld);
		model.addAttribute("msg", "预约成功");
		model.addAttribute("url", "/weixin/coach/itemShow?coachId=" + coachItem.getCoachId());

		return "/weixin/msg";
	}

	@RequestMapping(value = "coachConfig", method = RequestMethod.GET)
	public String coachConfig(Model model) {
		CoachConfig cc = coachConfigService.findCoachConfig();
		model.addAttribute("cc", cc);
		return "/weixin/coach/coachConfig";
	}
}
